package SM.UI;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import SM.BusinessLogic.DBAccess;
import SM.BusinessLogic.DBService;
import SM.UI.Controls.DataGrid;
import SM.UI.Controls.DropDown;
import SM.UI.Controls.TextBox;

/**
 * Servlet implementation class ReportIssue
 */

public class AdminCommittee extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public AdminCommittee() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// doGet is used to render HTML content
	
		/*Variable Declaration*/
		
		PrintWriter out = response.getWriter();
		SM.UI.Controls.DataGrid dgCommittee = new DataGrid("committee", 100, "t");
		SM.UI.Controls.TextBox txtMemberName = new TextBox("member-name", 30,"mem_name");
		SM.UI.Controls.DropDown ddResp = new DropDown("role", 350,"mem_resp");
		
		SM.UI.Controls.DropDown ddAddResp = new DropDown("resp", 200,"add_resp");
		SM.UI.Controls.DropDown ddRemoveMember = new DropDown("remove", 350,"rem_member");
		
		String AddMemberString = new String();
		String RemoveMemberString = new String();
		
		txtMemberName.SetReadOnly();
		
		request.getRequestDispatcher("page1.html").include(request, response);
		
		//display data grid 
		
		/*This should happen in isolation*/
		
		synchronized(this) { 
		
		/*Actual data grid data*/
		
				dgCommittee.SetChildren(txtMemberName, ddResp); // child1 is text box containing member's name
															   // child2 is drop down
				out.println(dgCommittee.getHTML());
				
				Connection connection = DBAccess.MakeConnection();
				CallableStatement statement = DBAccess.GetStatementForProcedureCall(connection, "ResponsibilitiesSelect()");
				
				ResultSet resultSet = DBService.ExecuteProcedure(connection, statement);
				
				ddAddResp.SetDataSource(resultSet,1 , 2);
				ddAddResp.DataBind();
				
				AddMemberString = ddAddResp.getHTML();
				
				statement = DBAccess.GetStatementForProcedureCall(connection, "MembersSelect()");
				
				resultSet = DBService.ExecuteProcedure(connection, statement);
				
				ddRemoveMember.SetDataSource(resultSet, 1, 1);
				ddRemoveMember.DataBind();
				
				RemoveMemberString = ddRemoveMember.getHTML();
				
				DBAccess.CloseConnection(connection);
				DBAccess.CloseStatement(statement);
				
		}
		
		out.println("<table width='100%' cellspacing=20>\n");
		
			out.println("<tr>\n");

				out.println("<td style='text-align:center;'>\n");
	
					out.println("<a href='javascript:void(0);' class='button_role' id='update_resp'>Update</a>\n");
		
				out.println("</td>\n");
		
			out.println("</tr>\n");
	
			out.println("<tr>\n");
		
				out.println("<td style='text-align:center;'>\n");

					out.println("<span id='msg_update_resp' style='display: none;'></span>");
	
				out.println("</td>\n");
	
			out.println("</tr>\n");
	
		out.println("</table>\n");
	
		//data grid display over
		
		out.println("<br class='clearfix' />\n");
		
		out.println("<table width='100%' cellspacing=20>\n");
		
			out.println("<tr>\n");
		
				out.println("<td class='head-t'>\n");
			
					out.println("<div style='text-align:center;'>Add a Member</div>\n");
				
				out.println("</td>\n");
				
			out.println("</tr>\n");
			
		out.println("</table>\n");
		
		/*Add member display row over*/
		
		out.println("<table width='100%' cellspacing=20>\n");
		
			out.println("<th>\n");
		
				out.println("<td style='text-align:center;'>\n");

					out.println("&nbsp");
	
				out.println("</td>\n");
				
				out.println("<td style='text-align:center;'>\n");

					out.println("Name");

				out.println("</td>\n");
				
				out.println("<td style='text-align:center;'>\n");

					out.println("Email");

				out.println("</td>\n");
			
				out.println("<td style='text-align:center;'>\n");

					out.println("Contact No.");

				out.println("</td>\n");
			
				out.println("<td style='text-align:center;'>\n");

					out.println("Responsibility");

				out.println("</td>\n");
			
			out.println("</th>\n");
			
			out.println("<tr>\n");
	
				out.println("<td>\n");
		
					out.println("Member's name");
			
				out.println("</td>\n");
			
				out.println("<td>\n");
				
					out.println("<select id='address'>" +
							"<option value='Mr.'> Mr. </option>" +
							"<option value='Ms.'> Ms. </option>" +
							"<option value='Mrs.'> Mrs. </option>" +
							" </select>\n");
		
				out.println("</td>\n");
			
				out.println("<td>\n");
				
					out.println("<input type='text' name='name' id='name' size=20 />");
		
				out.println("</td>\n");
				
				out.println("<td>\n");
				
					out.println("<input type='text' name='email' id='email' size=20 />");
	
				out.println("</td>\n");
			
				out.println("<td>\n");
				
					out.println("<input type='text' name='contact' id='contact' size=20 />");

				out.println("</td>\n");
		
			
				out.println("<td>\n");
				
					out.println(AddMemberString);
				
				out.println("</td>\n");
			
			out.println("</tr>\n");
		
		out.println("</table>\n");
	
		/*Add member info table over*/
		
		out.println("<table width='100%' cellspacing=20>\n");
		
			out.println("<tr>\n");
	
				out.println("<td style='text-align:center;'>\n");
		
					out.println("<a href='javascript:void(0);' class='button_role' id='save_member'>Save</a>\n");
			
				out.println("</td>\n");
			
			out.println("</tr>\n");
		
			out.println("<tr>\n");
			
				out.println("<td style='text-align:center;'>\n");
	
					out.println("<span id='msg_add_mem' style='display: none;'></span>");
		
				out.println("</td>\n");
		
			out.println("</tr>\n");
		
		out.println("</table>\n");
		
		/*Save member button over*/
		
		out.println("<br class='clearfix' />\n");
		
			out.println("<table width='100%' cellspacing=20>\n");
		
				out.println("<tr>\n");
		
					out.println("<td class='head-t'>\n");
			
						out.println("<div style='text-align:center;'>Remove a Member</div>\n");
				
					out.println("</td>\n");
				
				out.println("</tr>\n");
			
			out.println("</table>\n");
		
		/* Remove member display over*/
		
			out.println("<table width='100%' cellspacing=20>\n");
			
				out.println("<tr>\n");
	
					out.println("<td style='text-align:center;'>\n");
		
						out.println(RemoveMemberString);
			
					out.println("</td>\n");
			
				out.println("</tr>\n");
		
			out.println("</table>\n");
		
			/*Remove member string over*/
			
			out.println("<table width='100%' cellspacing=20>\n");
			
				out.println("<tr>\n");
	
					out.println("<td style='text-align:center;'>\n");
		
						out.println("<a href='javascript:void(0);' class='button_role' id='remove_member'>Remove</a>\n");
			
					out.println("</td>\n");
			
				out.println("</tr>\n");
		
				out.println("<tr>\n");
				
					out.println("<td style='text-align:center;'>\n");
	
						out.println("<span id='msg_rem_mem' style='display: none;'></span>");
		
					out.println("</td>\n");
		
				out.println("</tr>\n");
			
			out.println("</table>\n");
		
			/*Remove member button over*/
			
		request.getRequestDispatcher("page2.html").include(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// doPost is used to Accept request from page and Make Changes
		
		
		
	}

}
